<?php
class Admin extends CI_Controller {

	/**
	 * 显示登录页面
	 */
	public function index() {
		$this->load->view('admin/login.php');
	}

	/**
	 * 执行登录操作
	 */
	public function doLogin() {
		// 还有校验码检查

		$userName = $this->input->post('user_name');
		$password = $this->input->post('password');

		$this->admin_model->user_name = $userName;
		$this->admin_model->password = $password;

		$userId = $this->admin_model->authenticate();
		if($userId != "") {
			$gmtLastLogin = date("Y-m-d H:i:s", time());
			$lastIp = $this->input->ip_address();
			$this->admin_model->updateLastLoginInfo($userName, $gmtLastLogin, $lastIp);

			$this->session->set_userdata('userId', $userId);
			$this->session->set_userdata('userName', $userName);

			//echo site_url("/main");
			redirect(site_url("/main"));
		} else {
			$data['error_info'] = '错误的用户名/密码！';
			$this->load->view('admin/login.php', $data);
		}
	}

	/**
	 * 执行登出操作
	 */
	public function doLogout() {
		$this->session->sess_destroy();
		$this->load->view('admin/login.php');
	}

}